﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;

namespace Editor
{
    public partial class TileListWindow : Form
    {
        Image _Tiles;
        EditorCursor _cursor;
        PictureBox _tileBox = new PictureBox();
        Graphics _graphics;
        Graphics _cursor1Graphics;
        Bitmap _background;
        Size _boxSize = new Size(672, 384);
        Point _tileScroll = new Point(0, 0);
        BufferedGraphicsContext BGC;
        public delegate void GiveBitmap(Bitmap tileImage, int X, int Y);
        public event GiveBitmap TileSelected;
        private int _tileXLocation
        {
            get { return (_tileScroll.X / -32) + _cursor.OldX; }
        }
        private int _tileYLocation
        {
            get { return (_tileScroll.Y / -32) + _cursor.OldY; }
        }
        public TileListWindow(ref Image Tiles)
        {
            BGC = BufferedGraphicsManager.Current;
            _Tiles = Tiles;
            InitializeComponent();
            _background = new Bitmap(_Tiles);
            _graphics = Graphics.FromImage(_background);
            _cursor1Graphics = panel1.CreateGraphics();
            BGC.Allocate(_cursor1Graphics, new Rectangle(panel1.Location, panel1.Size));
            _cursor = new EditorCursor(ref _cursor1Graphics, ref _background);
            SetupCursor();
            DoubleBuffered = true;
        }
        private void SetupCursor()
        {
            _cursor.UpdatePosition(0, 0);
        }
        private void DrawScreen()
        {
            _graphics.Clear(Color.Black);
            _graphics.DrawImageUnscaledAndClipped(_Tiles, new Rectangle(_tileScroll, _boxSize));
            _graphics.Flush();
            SetupCursor();
        }
        private void TileListWindow_Activated(object sender, EventArgs e)
        {
            DrawScreen();
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            _cursor.UpdatePosition((int)(e.Location.X / 32), (int)(e.Location.Y / 32));
        }

        private void TileListWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.Hide();
                return;
            }
            else
            {
                BGC.Invalidate();
                BGC.Dispose();
            }
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            _tileScroll.X = e.NewValue * -32;
            _graphics.Clear(Color.Black);

            _graphics.DrawImage(_Tiles, new Rectangle(_tileScroll, _Tiles.Size));
            _graphics.Flush();
            _cursor.DrawRectangle1();
        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            _tileScroll.Y = e.NewValue * -32;
            _graphics.Clear(Color.Black);

            _graphics.DrawImage(_Tiles, new Rectangle(_tileScroll, _Tiles.Size));
            _graphics.Flush();
            _cursor.DrawRectangle1();
        }

        protected override void OnMouseWheel(MouseEventArgs e)
        {
            try
            {
                vScrollBar1.Value += e.Delta / -120;
            }
            catch { }
            base.OnMouseWheel(e);
        }

        private void vScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            _tileScroll.Y = vScrollBar1.Value * -32;
            _graphics.Clear(Color.Black);
            _graphics.DrawImage(_Tiles, new Rectangle(_tileScroll, _Tiles.Size));
            _graphics.Flush();
            _cursor.DrawRectangle1();
        }

        private void panel1_Click(object sender, EventArgs e)
        {
            Point drawPoint = new Point(-(_tileScroll.X - _cursor.OldX * 32), -(_tileScroll.Y - _cursor.OldY * 32));
            System.Drawing.Size size = new Size(32, 32);
            Bitmap image = new Bitmap(32, 32);
            Graphics g = Graphics.FromImage(image);
            //g.Clear(Color.Black);
            g.DrawImage(_Tiles, new Rectangle(new Point(0, 0), size), new Rectangle(drawPoint, size), GraphicsUnit.Pixel);
            // g.DrawImageUnscaled(_Tiles, new Rectangle(drawPoint, size));
            g.Dispose();
            this.TileSelected(image, _tileXLocation, _tileYLocation);
        }
        private void SendBitmapData()
        {
        }
        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Point drawPoint = new Point(-(_tileScroll.X - _cursor.OldX * 32), -(_tileScroll.Y - _cursor.OldY * 32));
            System.Drawing.Size size = new Size(32, 32);
            Bitmap image = new Bitmap(32, 32);
            Graphics g = Graphics.FromImage(image);
            //g.Clear(Color.Black);
            g.DrawImage(_Tiles, new Rectangle(new Point(0, 0), size), new Rectangle(drawPoint, size), GraphicsUnit.Pixel);
            // g.DrawImageUnscaled(_Tiles, new Rectangle(drawPoint, size));
            g.Dispose();
            this.TileSelected(image, _cursor.OldX, _cursor.OldY);
            this.Hide();
        }


    }
}
